package com.transport.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.classic.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.transport.model.Board;
import com.transport.utils.PageSet;

public class BoardDao extends HibernateDaoSupport{

	public String addBoard(Board board) {
		board.setIsshow("0");
		getHibernateTemplate().save(board);
		return "success";
	}

	public String checkExist(Board board) {
		List list = getHibernateTemplate().find(
				"from Board where title='" +board.getTitle() + "'");
		if (list.size() == 0) {
			return "none";
		}
		else {
			return "one";
		}
	}

	public Map queryCompanyList(PageSet pageSet) {
		Map map = new HashMap();
		String sql = "select count(*) from Board";
		Long total = (Long) getHibernateTemplate().find(sql).get(0);
		Session session = getHibernateTemplate().getSessionFactory().openSession();
		Query q = session.createQuery("from Board");
		q.setFirstResult((pageSet.getPage() - 1) * pageSet.getRows());
		q.setMaxResults(pageSet.getRows());
		List list = q.list();
		map.put("total", total);
		map.put("rows", list);
		session.close();
		return map;
	}

	public void deleteBoard(String id) {
		getHibernateTemplate().delete(getHibernateTemplate().find("from Board where id="+id).get(0));
	}

	public Board queryBoardById(String id) {
		return (Board)getHibernateTemplate().find("from Board where id="+id).get(0);
	}

	public String updateBoard(Board board) {
		Board info=(Board) getHibernateTemplate().find("from Board where id="+board.getId()).get(0);
		info.setContext(board.getContext());
		info.setIsshow(board.getIsshow());
		info.setPic(board.getPic());
		info.setTitle(board.getTitle());
		getHibernateTemplate().saveOrUpdate(info);
		return "success";
	}

	public Integer showNotice(String id) {
		Board temp = (Board)getHibernateTemplate().find("from Board where id="+id).get(0);
		temp.setIsshow("1");
		getHibernateTemplate().update(temp);
		return 1;
	}

}
